package com.example.medmobile;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.appengine.medmobileserver.business.ListaResultadoBusca;
import com.google.appengine.medmobileserver.business.Profissional;

public class ResultadoBuscaAdapter extends BaseAdapter {

	private Context context;
	private ListaResultadoBusca lista;
	private String sData;
	private String sHora;

	public ResultadoBuscaAdapter(Context context, ListaResultadoBusca lista,
			String sData, String sHora) {
		this.context = context;
		this.lista = lista;
		this.sData = sData;
		this.sHora = sHora;
		

	}

	public int getCount() {
		return lista.size();
	}

	public Object getItem(int i) {
		return lista.get(i);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int posicao, View converView, ViewGroup parent) {
		final com.google.appengine.medmobileserver.business.ResultadoBusca resultado = lista.get(posicao);
		final Profissional profissional = resultado.getP();
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.activity_resultado_busca_adapter,
				null);
		TextView textNome = (TextView) v.findViewById(R.id.AdapterBuscaNomeMedico);
		textNome.setText(profissional.getNome());
		TextView textEndereco = (TextView) v.findViewById(R.id.AdapterBuscaEndereco);
		textEndereco.setText(profissional.getEndereco());
		TextView textdistancia = (TextView) v.findViewById(R.id.AdapterBuscaDistancia);
		if (resultado.isBuscaGPS())
		{	
		textdistancia.setText(resultado.getsTextoDistancia());
		}
		else
			textdistancia.setText("");
		try {
			ImageButton btAgendar = (ImageButton) v
					.findViewById(R.id.AdapterBuscaBtAgendar);
			btAgendar.setTag(posicao);

			btAgendar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

				
					int iIDProfissional = profissional.getIdProfissional();

					
					Bundle param = new Bundle();				
					param.putInt("IdProfissional",iIDProfissional);
					param.putString("Hora", sHora);
					param.putString("Data", sData);

					Intent intent = new Intent();
					intent.putExtras(param);
					intent.setClass(context, HorarioAgendamento.class);
					 context.startActivity(intent);



				}
			});
		} catch (Exception e) {
			Log.i("EX1", e.getMessage());
		}
		
		ImageButton AdapterBuscaBtLigar = (ImageButton) v.findViewById(R.id.AdapterBuscaBtLigar);
		AdapterBuscaBtLigar.setTag(posicao);
		AdapterBuscaBtLigar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				String sFone = profissional.getTelefone();
				Uri uri = Uri.parse("tel:"+sFone);
				Intent it = new Intent(Intent.ACTION_CALL,uri);
				context.startActivity(it);
				
			}
		});

		v.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Profissional p = profissional ;
			 	 Intent intent = new Intent();
			 	Bundle param = new Bundle();
	

			 	String sProfissional = com.cedarsoftware.util.io.JsonWriter.toJson(p);
			 	
				param.putString("Profissional", sProfissional);
				param.putString("Data", sData);	
				intent.putExtras(param);
		         intent.setClass(  context,
		        		 PerfilMedico.class);
		         context.startActivity(intent);
		          
				
			}
		});
		return v;
	}
}
